﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using EpsiRisk.MoteurJeu;
using System.Threading;
using Microsoft.Xna.Framework.Content;

namespace EpsiRisk.AffichageJeu
{
    public class ClassZoneOverlayMessage : ClassZone
    {
        public string message = "Default Message";
        public Color printColor = Color.Black;
        private Thread threadFade;
        private bool abort = false;
        const float scale = 2;
        private int messageOffset = 0;


        public ClassZoneOverlayMessage()
        {
            height = 100;
            width = 600;
            position = new Vector2(100, 460);
            backcolor = Color.TransparentBlack;
            printColor.A = 0;
        }

        internal override void initialiser(ContentManager content)
        {

        }

        internal override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.DrawString(ClassDonneesGlobales.SpriteTxt, message, new Vector2(position.X+messageOffset, position.Y), printColor, 0, new Vector2(), scale, SpriteEffects.None, 0);
            spriteBatch.End();
        }

        private void FadeText()
        {
            while (printColor.A != 0 && !abort)
            {
                printColor.A--;
                Thread.Sleep(20);
            }
        }

        public void PopText(String s)
        {
            abort = true;
            if (threadFade != null)
            {
                threadFade.Join(50);
            }
            abort = false;
            message = s;
            int measurestring = (int)(ClassDonneesGlobales.SpriteTxt.MeasureString(s).X * scale);
            if (measurestring <= width)
            {
                messageOffset = (width - measurestring) / 2;
            }
            else
            {
                messageOffset = 0;
            }
            printColor.A = Byte.MaxValue;
            threadFade = new Thread(new ThreadStart(FadeText));
            threadFade.Start();
        }
    }
}
